package com.pachong.common.impl;

import com.common.core.utils.ReType;
import com.pachong.common.BaseService;
import com.pachong.common.CurrentUser;
import com.wy.dao.sys.BaseDAO;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * @author zhuxiaomeng
 * @date 2017/12/13.
 * @email 154040976@qq.com
 */
public abstract class BaseServiceImpl<T,E extends Serializable> implements BaseService<T,E> {

  private static Logger LOGGER= LoggerFactory.getLogger(BaseServiceImpl.class);

  public abstract BaseDAO<T,E> getBaseDao();


  @Override
  public void deleteByPrimaryKey(E id) {
     getBaseDao().delete(id);
  }

    @Override
    public void saveBatching(List<T> lists) {
        getBaseDao().save(lists);
    }

    @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public void save(T record) {
    if(record == null){
      LOGGER.error("save方法，请求参数为空！");
      return;
    }
    getBaseDao().save(record);
  }

  @Override
  public T selectByPrimaryKey(E id) {
    return getBaseDao().findOne(id);
  }

  @Override
  public T saveAndFlush(T record) {
    if(record == null){
      LOGGER.error("BaseServiceImpl saveAndFlush param is null!");
      return null;
    }
    return getBaseDao().saveAndFlush(record);
  }

  @Override
  public List<T> selectList(T record){
      Example<T> example = Example.of(record);
      return getBaseDao().findAll(example);
  }

  /**
   * 公共展示类
   * @param record 实体
   * @param page 页
   * @param limit 行
   * @return
   */
  @Override
  @Transactional
  public ReType<T> show(T record,int page,int limit){
      Example<T> example = Example.of(record);
      //划重点，laiui框架的分页第一页是1
      Pageable pageable = new PageRequest(page-1,limit);
      Page<T> all = getBaseDao().findAll(example, pageable);
      ReType<T> reType=new ReType(all.getTotalElements(),all.getContent());
      return reType;
  }


  @Override
  public Page<T> selectListByRecordAndPage(T record, Pageable pageable){
    if(record == null || record == null){
      LOGGER.error("selectListByRecordAndPage 请求参数为空！！！");
      return null;
    }
    Example<T> example = Example.of(record);
    return getBaseDao().findAll(example, pageable);
  }

  /**
   * 通用注入创建 更新信息 可通过super调用
   * @param record
   * @param flag
   * @return
   */
  public T  addValue(T record,boolean flag){
    CurrentUser currentUser= (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
    //统一处理公共字段
    Class<?> clazz=record.getClass();
    try {
      if(flag){
        Field field=clazz.getDeclaredField("createBy");
        field.setAccessible(true);
        field.set(record,currentUser.getId());
        Field fieldDate=clazz.getDeclaredField("createDate");
        fieldDate.setAccessible(true);
        fieldDate.set(record,new Date());
      }else{
        Field field=clazz.getDeclaredField("updateBy");
        field.setAccessible(true);
        field.set(record,currentUser.getId());
        Field fieldDate=clazz.getDeclaredField("updateDate");
        fieldDate.setAccessible(true);
        fieldDate.set(record,new Date());
      }
    } catch (NoSuchFieldException e) {
      //无此字段
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return record;
  }
}
